package 并发编程05.线程的等级;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * 作者：idea
 * 日期：2018/8/4
 * 描述：编写一个案例用于证明多线程的优先级不会真正决定线程的执行顺序
 */
public class Priority {

    private static volatile boolean notStart=true;
    private static volatile boolean notEnd=true;

    public static void main(String[] args) throws InterruptedException {
        List<Job> jobList=new ArrayList<Job>();
        for(int i=1;i<=10;i++){
            int priority=i<5?Thread.MIN_PRIORITY:Thread.MAX_PRIORITY;
            Job job=new Job(priority);
            jobList.add(job);
            Thread thread=new Thread(job,"thread----"+i);
            thread.setPriority(priority);
            thread.start();
        }

        notStart=false;
        TimeUnit.SECONDS.sleep(10);
        notEnd=false;
        System.out.println("all is start");
        for (Job job : jobList) {
            System.out.println("thread priority:"+job.priority+"  count is:"+job.count);
        }

    }

    static class Job extends Thread{

        private int count;
        private int priority;

        public Job(int priority){
            this.priority=priority;
        }

        @Override
        public void run() {
            super.run();
            while(notStart){
                Thread.yield();
            }
            while(notEnd){
                Thread.yield();
                count++;
            }
        }
    }
}
